package jscinttools.utilities.javautilities;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import java.lang.Double;

/**
 *  Some static methods for calculation
 *
 *@author     Marc-Antoine Verdier
 *@created    5 août 2011
 */
public class MathUtil{
	private MathUtil() { }

	/**
	 *  Calculate the uncorrelated error of the ratio a/b defined as
	 *  sqrt((da/b)²+(a*db/b²)²)
	 *
	 *@param  a   The value of a
	 *@param  da  The error on a
	 *@param  b   The value of b
	 *@param  db  The error on b
	 *@return     The error value of the ratio
	 */
	public static double uncorrRatioErr(double a, double da, double b, double db){
		return (double) sqrt(pow(da / b, 2) + pow(a * db / pow(b, 2), 2));
	}

	/**
	 *  Calculate the correlated error of the ratio a/b defined as
	 *  sqrt((da/b)²+(a*db/b²)²-2*a/b^3*da*db)
	 *
	 *@param  a   The value of a
	 *@param  da  The error on a
	 *@param  b   The value of b
	 *@param  db  The error on b
	 *@return     The error value of the ratio
	 */
	public static double corrRatioErr(double a, double da, double b, double db){
		return (double) sqrt(pow(da / b, 2) + pow(a * db / pow(b, 2), 2) - (2 * a / pow(b, 3) * da * db));
	}


	/**
	 *  Calculate the uncorrelated error of the product a*b defined as
	 *  sqrt((b*da)²+(a*db)²)
	 *
	 *@param  a   The value of a
	 *@param  da  The error on a
	 *@param  b   The value of b
	 *@param  db  The error on b
	 *@return     The error value of the ratio
	 */
	public static double uncorrProdErr(double a, double da, double b, double db){
		return (double) sqrt(pow(b * da, 2) + pow(a * db, 2));
	}

	/**
	 *  Calculate the correlated error of the product a*b defined as
	 *  sqrt((b*da)²+(a*db)² + 2*a*b*da*db)
	 *
	 *@param  a   The value of a
	 *@param  da  The error on a
	 *@param  b   The value of b
	 *@param  db  The error on b
	 *@return     The error value of the ratio
	 */
	public static double corrProdErr(double a, double da, double b, double db){
		return (double) sqrt(pow(b * da, 2) + pow(a * db, 2) + (2 * a * b * da * db));
	}
}
